/*
 *  Copyright 2012 SerGenis. All rights reserved.
 */
package com.sergenis.util;

import biz.source_code.base64Coder.Base64Coder;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

/**
 *
 * @author SerGenis
 */
public class SerialBase64 {

    public static String encode(Serializable s)
	throws IOException {
	ByteArrayOutputStream baos = new ByteArrayOutputStream();
	ObjectOutputStream oos = new ObjectOutputStream(baos);
	oos.writeObject(s);
	oos.close();
	return new String(Base64Coder.encode(baos.toByteArray()));
    }

    public static Object decode(String s)
	throws IOException, ClassNotFoundException {
	byte[] data = Base64Coder.decode(s);
	ByteArrayInputStream bais = new ByteArrayInputStream(data);
	ObjectInputStream ois = new ObjectInputStream(bais);
	Object o = ois.readObject();
	return o;
    }
}
